<template>
  <div class="text-editor">
    <div ref="editorToolbar" class="toolbar"></div>
    <div ref="editorContent" class="content"></div>
  </div>
</template>

<script>
import WangEditor from 'wangeditor';

export default {
  name: 'TextEditor',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      editor: null
    };
  },
  watch: {
    value(newValue) {
      if (this.editor && this.editor.txt.html() !== newValue) {
        this.editor.txt.html(newValue);
      }
    }
  },
  mounted() {
    this.initEditor();
  },
  beforeUnmount() {
    if (this.editor) {
      this.editor.destroy();
      this.editor = null;
    }
  },
  methods: {
    initEditor() {
      this.editor = new WangEditor(this.$refs.editorToolbar, this.$refs.editorContent);
      this.editor.config.onchange = () => {
        this.$emit('input', this.editor.txt.html());
      };
      this.editor.config.height = 400;
      this.editor.create();

      if (this.value) {
        this.editor.txt.html(this.value);
      }
    },
    getContent() {
      return this.editor.txt.html();
    },
    clearContent() {
      this.editor.txt.clear();
    }
  }
};
</script>

<style scoped>
.text-editor {
  border: 1px solid #ccc;
  border-radius: 5px;
  overflow: hidden;
}

.toolbar {
  background-color: #f5f5f5;
  border-bottom: 1px solid #ccc;
}

.content {
  min-height: 300px;
  padding: 10px;
  background-color: white;
  overflow-y: auto;
}
</style>
